﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using IdleTest.EFAndMVCDemo.IRepositories;
using IdleTest.EFAndMVCDemo.IServices;
using IdleTest.EFAndMVCDemo.Repositories;
using IdleTest.EFAndMVCDemo.Services;
using Microsoft.Practices.Unity;

namespace IdleTest.EFAndMVCDemo.MvcUI
{
    public class IocContainer
    {
        static IUnityContainer container;
        public static IUnityContainer Container
        {
            get { return container; }
        }

        public static void Register()
        {
            container = new UnityContainer();
            container.RegisterType<IUserService, UserService>();
            container.RegisterType<IUserRepository, UserRepository>();
        }

        public static T Instance<T>() where T : class
        {
            T t = container.Resolve<T>();
            if (t == null)
            {
                throw new NullReferenceException(string.Format("未注入【{0}】！", typeof(T)));
            }

            return t;
        }
    }
}